/****************************************************************************
 *
 *  ALi (Shanghai) Corporation, All Rights Reserved. 2008 Copyright (C)
 *
 *  File: win_copying.c
 *
 *  Description: copying UI
 *              
 *  History:
 *      Date        		Author         	Version   		Comment
 *      ====        		======        =======   	=======
 *	2.	2009.01.12		Sharon Zhang	0.1.001			initial for mediaplayer 3.0
 *
 ****************************************************************************/
#include <sys_config.h>
#ifdef USB_MP_SUPPORT
#include <types.h>
#include <osal/osal.h>
#include <api/libc/printf.h>
#include <api/libc/string.h>
#include <api/libosd/osd_lib.h>
#include <hld/snd/snd_dev.h>
#include <hld/snd/snd.h>
#include <api/libmp/media_player_api.h>
#include <errno.h>


#include "osdobjs_def.h"

#include "osd_config.h"
#include "string.id"
#include "images.id"

#include "control.h"
#include "win_com_popup.h"

/*******************************************************************************
*	WINDOW's objects declaration
*******************************************************************************/
extern CONTAINER 		g_win_copying;

extern TEXT_FIELD		copying_title;
extern TEXT_FIELD		copying_file;
extern PROGRESS_BAR		copying_bar;
extern TEXT_FIELD		copying_percent;

/*******************************************************************************
*	Macro definition
*******************************************************************************/
#define COPY_CON_SH_IDX	WSTL_POP_WIN_01_HD

#define COPY_TITLE_SH_IDX	WSTL_POP_TXT_SH_HD
#define B_BG_SH_IDX			WSTL_BARBG_01_8BIT
#define B_MID_SH_IDX		WSTL_NOSHOW_IDX
#define B_SH_IDX			WSTL_BAR_01_8BIT//WSTL_BAR_01_HD
#define COPY_TXT_SH_IDX		WSTL_TEXT_04_HD

#ifndef SD_UI
#define	WIN_COPY_L			224
#define	WIN_COPY_T			220//200
#define	WIN_COPY_W			560
#define	WIN_COPY_H 			200//240

#define	COPY_TITLE_L		(WIN_COPY_L+180)
#define	COPY_TITLE_T		(WIN_COPY_T+20)
#define	COPY_TITLE_W		200
#define	COPY_TITLE_H 		40

#define	COPY_FILE_L		(WIN_COPY_L+20)
#define	COPY_FILE_T		(COPY_TITLE_T+COPY_TITLE_H+20)
#define	COPY_FILE_W		(WIN_COPY_W-40)
#define	COPY_FILE_H 	40

#define	COPY_BAR_L		(WIN_COPY_L+20)
#define	COPY_BAR_T		(WIN_COPY_T+WIN_COPY_H-60)
#define	COPY_BAR_W		(WIN_COPY_W-130)
#define	COPY_BAR_H 		24

#define COPY_TXTP_L		(COPY_BAR_L + COPY_BAR_W)
#define COPY_TXTP_T 	(COPY_BAR_T - 6)
#define COPY_TXTP_W 	90
#define COPY_TXTP_H 	40
#else
#define	WIN_COPY_L			120
#define	WIN_COPY_T			220//200
#define	WIN_COPY_W			400
#define	WIN_COPY_H 			200//240

#define	COPY_TITLE_L		(WIN_COPY_L+180)
#define	COPY_TITLE_T		(WIN_COPY_T+20)
#define	COPY_TITLE_W		200
#define	COPY_TITLE_H 		40

#define	COPY_FILE_L		(WIN_COPY_L+20)
#define	COPY_FILE_T		(COPY_TITLE_T+COPY_TITLE_H+20)
#define	COPY_FILE_W		(WIN_COPY_W-40)
#define	COPY_FILE_H 	40

#define	COPY_BAR_L		(WIN_COPY_L+20)
#define	COPY_BAR_T		(WIN_COPY_T+WIN_COPY_H-60)
#define	COPY_BAR_W		(WIN_COPY_W-130)
#define	COPY_BAR_H 		16

#define COPY_TXTP_L		(COPY_BAR_L + COPY_BAR_W)
#define COPY_TXTP_T 	(COPY_BAR_T - 6)
#define COPY_TXTP_W 	90
#define COPY_TXTP_H 	40
#endif

/*******************************************************************************
*	Function decalare
*******************************************************************************/
static VACTION copying_keymap(POBJECT_HEAD pObj, UINT32 key);
static PRESULT copying_callback(POBJECT_HEAD pObj, VEVENT event, UINT32 param1, UINT32 param2);


/*******************************************************************************
*	Objects Macro
*******************************************************************************/
#define LDEF_WIN(varCon,nxtObj,l,t,w,h,focusID)		\
		DEF_CONTAINER(varCon,NULL,NULL,C_ATTR_ACTIVE,0, \
	    0,0,0,0,0, l,t,w,h, COPY_CON_SH_IDX,COPY_CON_SH_IDX,COPY_CON_SH_IDX,COPY_CON_SH_IDX,   \
		copying_keymap,copying_callback,  \
	    nxtObj, focusID,0)

#define LDEF_TITLE_TXT(root,varTxt,nxtObj,l,t,w,h,sh,resID)		\
    DEF_TEXTFIELD(varTxt,root,nxtObj,C_ATTR_ACTIVE,0, \
    0,0,0,0,0, l,t,w,h, sh,sh,sh,sh,   \
    NULL,NULL,  \
    C_ALIGN_CENTER| C_ALIGN_VCENTER, 0,0,resID,NULL)

#define LDEF_COPY_FILE(root,varTxt,nxtObj,l,t,w,h,sh,str)		\
    DEF_TEXTFIELD(varTxt,root,nxtObj,C_ATTR_ACTIVE,0, \
    0,0,0,0,0, l,t,w,h, sh,sh,sh,sh,   \
    NULL,NULL,  \
    C_ALIGN_LEFT| C_ALIGN_VCENTER, 0,0,0,str)

#define LDEF_COPY_BAR(root,varBar,nxtObj,l,t,w,h,rcl,rct,rcw,rch)	\
		DEF_PROGRESSBAR(varBar, root, nxtObj, C_ATTR_ACTIVE, 0, \
		0,0,0,0,0, l,t,w,h, B_BG_SH_IDX, B_BG_SH_IDX, B_BG_SH_IDX, B_BG_SH_IDX,\
		NULL, NULL, \
		PROGRESSBAR_HORI_NORMAL|PBAR_STYLE_RECT_STYLE, 0, 0, B_MID_SH_IDX, B_SH_IDX, \
		rcl,rct,rcw,rch, 1, 100, 100, 1)

#define LDEF_COPY_TXT(root,varTxt,nxtObj,l,t,w,h,sh,str)		\
    DEF_TEXTFIELD(varTxt,root,nxtObj,C_ATTR_ACTIVE,0, \
    0,0,0,0,0, l,t,w,h, sh,sh,sh,sh,   \
    NULL,NULL,  \
    C_ALIGN_RIGHT| C_ALIGN_VCENTER, 0,0,0,str)

/*******************************************************************************
*   WINDOW's objects defintion
*******************************************************************************/

LDEF_WIN(g_win_copying, &copying_title, WIN_COPY_L, WIN_COPY_T, WIN_COPY_W, WIN_COPY_H, 0)

LDEF_TITLE_TXT(&g_win_copying, copying_title, &copying_file,\
		COPY_TITLE_L,  COPY_TITLE_T,  COPY_TITLE_W,  COPY_TITLE_H,  COPY_TITLE_SH_IDX,  RS_COPYING)

LDEF_COPY_FILE(&g_win_copying, copying_file, &copying_bar,\
		COPY_FILE_L,  COPY_FILE_T,  COPY_FILE_W,  COPY_FILE_H,  COPY_TXT_SH_IDX,  display_strs[0])
#ifndef SD_UI
LDEF_COPY_BAR(&g_win_copying,  copying_bar, &copying_percent, \
		COPY_BAR_L, COPY_BAR_T, COPY_BAR_W, COPY_BAR_H, \
		2, 5, COPY_BAR_W, COPY_BAR_H-10)
#else
LDEF_COPY_BAR(&g_win_copying,  copying_bar, &copying_percent, \
		COPY_BAR_L, COPY_BAR_T, COPY_BAR_W, COPY_BAR_H, \
		2, 2, COPY_BAR_W, COPY_BAR_H-4)
#endif
LDEF_COPY_TXT(&g_win_copying, copying_percent, NULL,\
		COPY_TXTP_L,  COPY_TXTP_T,  COPY_TXTP_W,  COPY_TXTP_H,  COPY_TXT_SH_IDX,  display_strs[1])

/*******************************************************************************
*	Local functions & variables declare
*******************************************************************************/
#define VACT_EXIT_COPY (VACT_PASS + 1)

#define F_CP_END			0
#define F_CP_FAIL			1
#define F_CP_NEW_FILE		2
#define F_CP_PROGRESS		3

//typedef void (*filecopy_callback)(unsigned long param);
typedef void(*cp_callback_func)(unsigned long type, unsigned long param);

FileListHandle copyfile_list;
UINT32 copyfile_idx;
char copyfile_dest_path[FULL_PATH_SIZE];
BOOL g_from_copyfile = FALSE;

void win_filecopy_callback(unsigned long type, unsigned long param);
static void win_copying_init();
static PRESULT 	copyfile_unkown_act_proc(VACTION act);
static PRESULT filecopy_message_proc(UINT32 msg_type, UINT32 msg_code);

static VACTION copying_keymap(POBJECT_HEAD pObj, UINT32 key)
{
	VACTION act;

	switch(key)
	{
	case V_KEY_EXIT:
		act = VACT_EXIT_COPY;
		break;
	default:
		act = VACT_PASS;
	}

	return act;
}

static PRESULT copying_callback(POBJECT_HEAD pObj, VEVENT event, UINT32 param1, UINT32 param2)
{
	PRESULT ret = PROC_PASS;
	VACTION unact;
	UINT8 back_saved;
	win_popup_choice_t choice;
	RET_CODE func_ret = RET_SUCCESS;
	win_popup_choice_t type_choice;

	switch(event)
	{
   	case EVN_PRE_OPEN:
		win_copying_init();
		break;
   	case EVN_POST_OPEN:
		func_ret=copy_files_from_file_list(copyfile_list, copyfile_idx, copyfile_dest_path, 4, win_filecopy_callback);
		if(func_ret == RET_FAILURE)
		{
			ap_send_msg(CTRL_MSG_SUBTYPE_STATUS_COPYFILE_ERR, 1, TRUE);
		}
		else if(func_ret == RET_STA_ERR)
		{
			ap_send_msg(CTRL_MSG_SUBTYPE_STATUS_COPYFILE_CANT_COPY, 1, TRUE);
		}
		else if(func_ret == -16)
		{
			ap_send_msg(CTRL_MSG_SUBTYPE_STATUS_COPYFILE_OVERWRITE, 1, TRUE);
		}
		else if(func_ret == -1)
		{
			ap_send_msg(CTRL_MSG_SUBTYPE_STATUS_COPYFILE_ERR, 1, TRUE);
		}

		break;
	case EVN_PRE_CLOSE:
		g_from_copyfile = TRUE;
		/* Make OSD not flickering */
		*((UINT32*)param2) &= ~C_CLOSE_CLRBACK_FLG;	
		break;
	case EVN_UNKNOWN_ACTION:
		unact = (VACTION)(param1>>16);
		ret = copyfile_unkown_act_proc(unact);
		
		break;
	case EVN_MSG_GOT:
		ret = filecopy_message_proc(param1,param2); 
		break;
	default:
		break;
	}
		
	return ret;
}
/*
void win_copying_open(void)
{
	UINT32 hkey;
	CONTAINER	*pop_win;
	TEXT_FIELD *pTxt;
	PRESULT ret = PROC_LOOP;

    BOOL old_value = ap_enable_key_task_get_key(TRUE);

	pop_win = &g_win_copying;
	wincom_backup_region(&pop_win->head.frame);
	OSD_ObjOpen((POBJECT_HEAD)pop_win, 0);
	while(ret != PROC_LEAVE)
	{
		hkey = ap_get_key_msg();
		if(hkey == INVALID_HK || hkey == INVALID_MSG)
			continue;

		ret = OSD_ObjProc((POBJECT_HEAD)pop_win, (MSG_TYPE_KEY<<16),hkey, 0);
	}
	wincom_restore_region();
    ap_enable_key_task_get_key(old_value);
}
*/
static void win_copying_init()
{
	TEXT_FIELD *pTxt;
	PROGRESS_BAR* pBar;
	
	pTxt = &copying_file;
	OSD_SetTextFieldContent(pTxt,STRING_ANSI,(UINT32)"");
	pTxt = &copying_percent;
	OSD_SetTextFieldContent(pTxt,STRING_ANSI,(UINT32)"");
	pBar = &copying_bar;
	OSD_SetProgressBarPos(pBar,0);
}

static PRESULT 	copyfile_unkown_act_proc(VACTION act)
{
	PRESULT ret = PROC_LOOP;
	UINT8 back_saved;
	win_popup_choice_t choice;
	
	switch(act)
	{
	case VACT_EXIT_COPY:
		win_compopup_init(WIN_POPUP_TYPE_SMSG);
		#ifndef SD_UI
		win_compopup_set_frame(496, 330, 286, 130);
		#else
		win_compopup_set_frame(200, 330, 286, 130);
		#endif
		win_compopup_set_msg("Copy canceled!", NULL,0);
		win_compopup_open_ext(&back_saved);
			f_cancel_cp();
		osal_task_sleep(500);
		win_compopup_smsg_restoreback();
		ret = PROC_LEAVE;
		break;
	default:

		break;
	}

	return ret;
}

void win_filecopy_callback(unsigned long type, unsigned long param)
{
	UINT32 precent;
	UINT32 newfile;
	
	switch(type)
	{
		case F_CP_END:
			ap_send_msg(CTRL_MSG_SUBTYPE_CMD_EXIT, 1, TRUE);
			
			break;

		case F_CP_FAIL:
			ap_send_msg(CTRL_MSG_SUBTYPE_STATUS_COPYFILE_ERR, param, TRUE);
			break;

		case F_CP_NEW_FILE:
			newfile = (unsigned long)param;
			ap_send_msg(CTRL_MSG_SUBTYPE_STATUS_COPYFILE_NEWFILE,newfile,FALSE);

			break;

		case F_CP_PROGRESS:
			precent = (unsigned long)param;
			ap_send_msg(CTRL_MSG_SUBTYPE_STATUS_COPYFILE_PROGRESS,precent,FALSE);

			break;
			
		default:

			break;

	}

}

static PRESULT	filecopy_message_proc(UINT32 msg_type, UINT32 msg_code)
{
	PRESULT ret = PROC_LOOP;
	UINT8 back_saved;
	UINT32 hkey = INVALID_HK;
	TEXT_FIELD *pFile;
	TEXT_FIELD *pTxt;
	PROGRESS_BAR* pBar;
	char str_buff[FULL_PATH_SIZE];
	win_popup_choice_t choice;
	RET_CODE func_ret = RET_SUCCESS;

 	switch(msg_type)
	{
	case CTRL_MSG_SUBTYPE_CMD_EXIT:
		win_compopup_init(WIN_POPUP_TYPE_SMSG);
		#ifndef SD_UI
		win_compopup_set_frame(496, 330, 286, 130);
		#else
		win_compopup_set_frame(200, 330, 286, 130);
		#endif
		win_compopup_set_msg("Copy finished!", NULL,0);
		win_compopup_open_ext(&back_saved);
		osal_task_sleep(1000);
		win_compopup_smsg_restoreback();
		ret = PROC_LEAVE;
		break;

	case CTRL_MSG_SUBTYPE_STATUS_COPYFILE_ERR:
		win_compopup_init(WIN_POPUP_TYPE_SMSG);
		#ifndef SD_UI
		win_compopup_set_frame(496, 330, 286, 130);
		#else
		win_compopup_set_frame(200, 330, 286, 130);
		#endif
		if (msg_code == (UINT32)(-ENOSPC))
			win_compopup_set_msg("No space, copy failed!", NULL,0);
		else
			win_compopup_set_msg(NULL, NULL,RS_MSG_FILE_COPY_FAILED);
		win_compopup_open_ext(&back_saved);
		osal_task_sleep(1000);
		win_compopup_smsg_restoreback();
		ret = PROC_LEAVE;
		break;

	case CTRL_MSG_SUBTYPE_STATUS_COPYFILE_CANT_COPY:
		win_compopup_init(WIN_POPUP_TYPE_SMSG);
		#ifndef SD_UI
		win_compopup_set_frame(496, 330, 286, 130);
		#else
		win_compopup_set_frame(200, 330, 286, 130);
		#endif
		win_compopup_set_msg("Can't copy!", NULL,0);
		win_compopup_open_ext(&back_saved);
		osal_task_sleep(1000);
		win_compopup_smsg_restoreback();
		ret = PROC_LEAVE;
		break;

	case CTRL_MSG_SUBTYPE_STATUS_COPYFILE_OVERWRITE:
		win_compopup_init(WIN_POPUP_TYPE_OKNO);
		win_compopup_set_msg("Do you want to overwrite?",NULL,0);
		choice = win_compopup_open_ext(&back_saved);		

		if(choice == WIN_POP_CHOICE_YES)
		{
			func_ret = copy_files_from_file_list(copyfile_list, copyfile_idx, copyfile_dest_path, 8,win_filecopy_callback);
			if(func_ret == -1)
			{
				win_compopup_init(WIN_POPUP_TYPE_SMSG);
				#ifndef SD_UI
				win_compopup_set_frame(496, 330, 286, 130);
				#else
				win_compopup_set_frame(200, 330, 286, 130);
				#endif
				win_compopup_set_msg(NULL, NULL,RS_MSG_FILE_COPY_FAILED);
				win_compopup_open_ext(&back_saved);
				osal_task_sleep(1000);
				win_compopup_smsg_restoreback();
				ret = PROC_LEAVE;
			}
		}
		else
		{
			ret = PROC_LEAVE;
		}
		break;
			
	case CTRL_MSG_SUBTYPE_STATUS_COPYFILE_NEWFILE:
		pFile = &copying_file;
		win_get_display_dirname(str_buff, msg_code);
		OSD_SetTextFieldContent(pFile,STRING_UTF8,(UINT32)str_buff);
		OSD_DrawObject((POBJECT_HEAD)pFile,C_UPDATE_ALL);
	
		pTxt = &copying_percent;
		OSD_SetTextFieldContent(pTxt,STRING_ANSI,(UINT32)"");
		OSD_DrawObject((POBJECT_HEAD)pTxt,C_UPDATE_ALL);	
		pBar = &copying_bar;
		OSD_SetProgressBarPos(pBar,0);
		OSD_DrawObject((POBJECT_HEAD)pBar,C_UPDATE_ALL);
		break;

	case CTRL_MSG_SUBTYPE_STATUS_COPYFILE_PROGRESS:
		if(msg_code > 100)
			msg_code = 0;
	 
		pBar = &copying_bar;
		OSD_SetProgressBarPos(pBar,msg_code);
		OSD_DrawObject((POBJECT_HEAD)pBar,C_UPDATE_ALL);
		pTxt = &copying_percent;
		OSD_SetTextFieldContent(pTxt, STRING_NUM_PERCENT,msg_code);
		OSD_DrawObject((POBJECT_HEAD)pTxt,C_UPDATE_ALL);	
		break;

	default:
		break;
 	}

	return ret;
}



#endif




